# -*- coding: utf-8 -*-
#----------------------------------------------------------------------------#
# ZhihuiJin <zhihuijin@gmail.com>
# vim: ts=4 sw=4 sts=4 et tw=78:
# Fri Dec  8 14:46:39 2006
#
#----------------------------------------------------------------------------#

""" An interface to Han character list
"""

#----------------------------------------------------------------------------#

import os
import re
from www import settings
from util import readList, commList
from dict import hanziTable

#----------------------------------------------------------------------------#

_charListDir = os.path.join(settings.DATA_DIR, 'lists', 'char')

ch_gb2312               = os.path.join(_charListDir, 'ch_gb2312')
ch_gb2312_level1        = os.path.join(_charListDir, 'ch_gb2312_level1')
ch_common_char_level1   = os.path.join(_charListDir, 'ch_common_char_level1')
ch_common_char_level2   = os.path.join(_charListDir, 'ch_common_char_level2')

jp_sjis                 = os.path.join(_charListDir, 'jp_sjis')
jp_common_char_joyo     = os.path.join(_charListDir, 'jp_common_char_joyo')
jp_common_char_jinmeiyo = os.path.join(_charListDir, 'jp_common_char_jinmeiyo')

#----------------------------------------------------------------------------#
def getGB2312():
    """ get GB2312 charset
    """
    return readList(ch_gb2312)

#----------------------------------------------------------------------------#
def getGB2312Level1():
    """ get GB2312 charset, level-1
    """
    return readList(ch_gb2312_level1)

#----------------------------------------------------------------------------#
def getChCommonLevel1():
    """ get commonly used hanzi, level 1
    """
    return readList(ch_common_char_level1)

#----------------------------------------------------------------------------#
def getChCommonLevel2():
    """ get commonly used hanzi, level 2
    """
    return readList(ch_common_char_level2)

#----------------------------------------------------------------------------#
def getChCommonChar():
    """ get commonly used hanzi set, including level-1 and level-2
    """
    list =  getChCommonLevel1()
    list +=  getChCommonLevel2()
    return list

#----------------------------------------------------------------------------#
def getSJIS():
    """ get SJIS set
    """
    return readList(jp_sjis)

#----------------------------------------------------------------------------#
def getJpJoyo():
    """ get joyo kanji set
    """
    return readList(jp_common_char_joyo)

#----------------------------------------------------------------------------#
def getJpJinmeiyo():
    """ get jinmeiyo kanji set
    """
    return readList(jp_common_char_jinmeiyo)

#----------------------------------------------------------------------------#
def getJpCommonChar():
    """ get commonly used kanji, including joyo and jinmeiyo kanjis
    """
    list =  getJpJoyo()
    list +=  getJpJinmeiyo()
    return list

#----------------------------------------------------------------------------#
def getSharedCommonChar():
    """ get shared han characters in commonly used  kanji and hanzi set
    """
    chComm = getChCommonChar()
    jpComm = getJpCommonChar()
    sharedSet = set(chComm).intersection(set(jpComm))
    return list(sharedSet)

#----------------------------------------------------------------------------#
def getChCommonOnly():
    """ get common hanzi which not exist in common kanji
    """
    return set(getChCommonChar()) - set(getJpCommonChar())


#----------------------------------------------------------------------------#
def getJpCommonOnly():
    """ get common kanji which not exist in common hanzi
    """
    return set(getJpCommonChar()) - set(getChCommonChar())


#----------------------------------------------------------------------------#
def getHanziCognateForKanji(kanjiSet):
    """ Get hanzi cognates for kanji set
    @param kanjiSet the kanji set to extract cognates
    """
    relation    = hanziTable.getCognateRelation()
    kanjiMapDic = relation.forwardMap()
    hanziSet    = set()
    for kanji in commList(kanjiMapDic.keys(), kanjiSet):
        for hanzi in kanjiMapDic[kanji]:
            hanziSet.add(hanzi)

    return list(hanziSet)

#----------------------------------------------------------------------------#
def getKanjiCognateForHanzi(hanziSet):
    """ Get kanji cognates for hanzi  set
    @param hanziSet the hanzi set to extract cognates
    """
    relation    = hanziTable.getCognateRelation()
    hanziMapDic = relation.reverseMap()
    kanjiSet    = set()
    for hanzi in commList(hanziMapDic.keys(), hanziSet):
        for kanji in hanziMapDic[hanzi]:
            kanjiSet.add(kanji)

    return list(kanjiSet)


#----------------------------------------------------------------------------#
def getPredictableHanzi(charSet, code):
    """ Get hanzi nonexistent in common Japanese charset but predictable for
    Japanese people 

    @param charSet the charset to extract similar cognate
    @param code    the similar level to be extracted
    """
    resultSet      = set()
    relation       = hanziTable.getProofedCognateRelation(code)
    kanjiMapDic    = relation.forwardMap()

    for kanji in commList(kanjiMapDic.keys(), charSet):
        for elem in kanjiMapDic[kanji]: 
            resultSet.add(elem)

    return list(resultSet)


#----------------------------------------------------------------------------#
def getPredictableKanji(charSet, code):
    """ Get kanji nonexistent in common Chinese charset but predictable for
    Chinese people 

    @param charSet the charset to extract similar cognate
    @param code    the similar level to be extracted
    """
    resultSet     = set()
    relation      = hanziTable.getProofedCognateRelation(code)
    hanziMapDic   = relation.reverseMap()

    for hanzi in commList(hanziMapDic.keys(), charSet):
        for elem in hanziMapDic[hanzi]: 
            resultSet.add(elem)

    return list(resultSet)

#----------------------------------------------------------------------------#
def getReadableHanziBaseline():
    """ Get commonly used hanzi which is indentical or highliy similar to one
    of its kanji cognates in commonly used Japanese kanjis, this set is
    regarded as a baseline of readable hanzi set for Japanese people 
    """
    commKanjiList = getJpCommonChar()
    predictList   = getPredictableHanzi(commKanjiList, 'q')
    commHanziList = getChCommonChar()
    return commList(commKanjiList + predictList, commHanziList)
    #return list(set(commKanjiList + predictList))

#----------------------------------------------------------------------------#
def getReadableKanjiBaseline():
    """ Get commonly used kanji which is indentical or highliy similar to one
    of its hanzi cognates in commonly used Chinese hanzis , this set is
    regarded as a baseline of readable kanji set for Chinese people 
    """
    commHanziList = getChCommonChar()
    predictList   = getPredictableKanji(commHanziList, 'q')
    commKanjiList = getJpCommonChar()
    return commList(commHanziList + predictList, commKanjiList)
    #return list(set(commHanziList + predictList))

#----------------------------------------------------------------------------#
def getReadableHanziTopline():
    """ Get commonly used hanzi whose kanji cognate is in commonly used
    Japanese kanjis, this set is regarded as a topline of readable hanzi set
    for Japanese people 
    """
    commKanjiList = getJpCommonChar()
    cognateList   = getHanziCognateForKanji(commKanjiList)
    commHanziList = getChCommonChar()
    return commList(commKanjiList + cognateList, commHanziList)
    #return list(set(commKanjiList + cognateList))

#----------------------------------------------------------------------------#
def getReadableKanjiTopline():
    """ Get commonly used kanji whose hanzi cognate is in commonly used
    Chinese hanzis, this set is regarded as a topline of readable kanji set
    for Chinese people 
    """
    commHanziList = getChCommonChar()
    cognateList   = getKanjiCognateForHanzi(commHanziList)
    commKanjiList = getJpCommonChar()
    return commList(commHanziList + cognateList, commKanjiList)
    #return list(set(commHanziList + cognateList))

#----------------------------------------------------------------------------#
